package kr.saeildamil.androidstudy.ch13;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListPopupWindow;

public class ListPopupWindowTest extends Activity {
	Button mButton;
	ListPopupWindow mListPopupWindow;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch13_listpopupwindowtest);
		ArrayAdapter<CharSequence> Colors = ArrayAdapter.createFromResource(this,  R.array.Colors, android.R.layout.simple_list_item_1);

		mButton = (Button) findViewById(R.id.ch13_listpopupwindowtest_btn);
		mListPopupWindow = new ListPopupWindow(this);
		mListPopupWindow.setWidth(300);
		mListPopupWindow.setHeight(300);
		mListPopupWindow.setAnchorView(mButton);
		mListPopupWindow.setAdapter(Colors);
		mListPopupWindow.setModal(true);

		mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch(position){
				case 0: mButton.setBackgroundColor(Color.RED); break;
				case 1: mButton.setBackgroundColor(Color.GREEN); break;
				case 2: mButton.setBackgroundColor(Color.BLUE); break;
				case 3: mButton.setBackgroundColor(Color.YELLOW); break;
				case 4: mButton.setBackgroundColor(Color.CYAN); break;
				case 5: mButton.setBackgroundColor(Color.MAGENTA); break;
				}
			}
		});
	}
	
	public void mOnClick(View v){
		if(mListPopupWindow.isShowing()){
			mListPopupWindow.dismiss();
		} else { 
			mListPopupWindow.show();
		}
	}

}
